package com.umeng.community.example.Utils;

import android.app.Activity;
import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.qianxx.base.IConstants;
import com.qianxx.base.Urls;
import com.qianxx.base.loading.LoadingControl;
import com.qianxx.base.request.Config;
import com.qianxx.base.request.RM;
import com.qianxx.base.request.RequestBean;
import com.qianxx.base.request.RequestCallback;
import com.qianxx.base.request.RequestUtil;
import com.qianxx.base.utils.SPUtil;
import com.qianxx.base.utils.ToastUtil;
import com.umeng.comm.ui.imagepicker.util.BroadcastUtils;

import java.util.HashMap;

/**
 * Created by zhangbingyong on 16/5/19.
 */
public class ThirdLoginOutRequestUtils implements RequestCallback {
    private ThirdLoginOutRequestUtils() {
    }

    private static ThirdLoginOutRequestUtils requestUtils;
    private static Context mContext;
    protected RequestQueue mRequest; // 请求队列对象
    protected LoadingControl mControl; // 用于控制loading指示
    protected static Activity mActivity;


    public static ThirdLoginOutRequestUtils getInstance(Context context,Activity activity) {

        if (requestUtils == null) {
            synchronized (ThirdLoginOutRequestUtils.class) {
                if (requestUtils == null) {
                    requestUtils = new ThirdLoginOutRequestUtils();

                }
            }
        }
        mContext = context;
        mActivity = activity;

        return requestUtils;
    }

    /**
     * 第三方登录退出后，退出服务器
     */
    public void ThirdLoginOut(){
        HashMap<String,String> map = new HashMap<>();
        requestData(IConstants.REQUEST, Urls.THIRDLOGINOUT_LIST, RM.POST, RequestBean.class, map);
    }

    @Override
    public void requestSuccess(RequestBean baseBean, Config config) {
        ToastUtil.getInstance().toast("退出登录");
        SPUtil.getInstance().setToken("");
        BroadcastUtils.sendUserLogoutBroadcast(mContext.getApplicationContext());
        mActivity.finish();
//        mContext.startActivity(new Intent(mContext, MainActivity.class));
    }

    /**
     * 查找"loading指示"控件
     */
    protected void findLoading() {
        if (mControl == null) {
            mControl = new LoadingControl(mActivity);
        }
    }

    /**
     * 显示"loading指示"
     * <p/>
     * isFullScreen = true，表示title部分也不可点击。
     */
    public void showLoading(boolean isFullScreen) {
        findLoading();
        // 默认title部分可以点击
        mControl.showLoading(new Config().setIsFullScreen(isFullScreen));
    }

    /**
     * 显示"loading指示"
     */
    public void showLoading() {
        showLoading(false);
    }

    /**
     * @param requestTag -请求标签（用于区分不同请求）
     * @param url        -配置的api接口
     * @param method     -请求方法（目前仅支持GET，POST）
     * @param bean       -返回的数据类型
     * @param params     -参数信息
     */
    protected void requestData(String requestTag, String url, RM method,
                               Class<? extends RequestBean> bean, HashMap<String, String> params) {
        showLoading(); // 显示Loading指示，默认title部分可以点击
        if (mRequest == null) {
            mRequest = Volley.newRequestQueue(mContext);
        }
        RequestUtil.requestData(mRequest, requestTag, url, method, bean,
                params, this);
    }

    /**
     * @param requestTag       -请求标签（用于区分不同请求）
     * @param url              -配置的api接口
     * @param method           -请求方法（目前仅支持GET，POST）
     * @param bean             -返回的数据类型
     * @param params           -参数信息
     * @param isTopUnclickable -为true时，title部分不可点击；为false时，title部分的按键可以点击(比如返回键)
     */
    protected void requestData(String requestTag, String url, RM method,
                               Class<? extends RequestBean> bean, HashMap<String, String> params,
                               boolean isTopUnclickable) {
        showLoading(isTopUnclickable); // 显示Loading指示，默认title部分可以点击
        if (mRequest == null) {
            mRequest = Volley.newRequestQueue(mContext);
        }
        RequestUtil.requestData(mRequest, requestTag, url, method, bean,
                params, this);
    }
    /**
     * 不带loading指示的数据请求
     */
    private void requestDataWithoutLoading(String requestTag, String url,
                                             RM method, Class<? extends RequestBean> bean,
                                             HashMap<String, String> params) {
        if (mRequest == null) {
            mRequest = Volley.newRequestQueue(mContext);
        }
        RequestUtil.requestData(mRequest, requestTag, url, method, bean,
                params, this, new Config().setShowLoading(false));
    }


    @Override
    public void requestFail(RequestBean bean, Config config) {

    }

    @Override
    public void requestTokenInvalid(String requestTag, Config config) {

    }
}
